﻿using System;
using System.Web.Security;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.views;
using com.eforceglobal.crux.controllers;
using com.eforceglobal.crux.security;
using com.eforceglobal.crux.configuration;

/// <summary>
/// Summary description for BaseView
/// </summary>
public class BaseView : System.Web.UI.Page, IBaseView
{
       public BaseView()
       {
           PreInit += BaseView_PreInit;
           ViewName = "";
       }


    protected void BaseView_PreInit(object sender, EventArgs e)
       {
           ICruxSecurityManager cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
           SessionData sessionData = new SessionData
                                         {
                                             CurrentView = ViewName,
                                             CurrentViewSection = NavigationController.GetSectionName(ViewName)
                                         };

        //TODO: Remove in Production Or Deployment Based Enivornment
        //   sessionData.CurrentUser = cruxSecurityManager.GetUserFromCompany("crux@eforceglobal.com", "Some company");

        if (sessionData.CurrentUser == null || 
            !cruxSecurityManager.HasAccessToView(sessionData.CurrentUser.UserName,
               sessionData.CurrentUser.CompanyName, ViewName))
               FormsAuthentication.RedirectToLoginPage();

           if (!UsesPageData)
               sessionData.PageData = null;
           
           
       }

        #region IBaseView Members
        public string ViewName { get; set; }

        string _ViewTitle = "";
        public string ViewTitle
        {
            get
            {
                if (string.IsNullOrEmpty(_ViewTitle))
                    return CruxConfigurationManager.GetViewTitle(ViewName);
                return _ViewTitle;
            }
            set
            {
                _ViewTitle = value;
            }
        }

        #endregion

        #region IBaseView Members

    public bool UsesPageData { get; set; }

    #endregion
}
